package com.permission.advice;

import com.permission.exception.*;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

/**
 * Created by niuniu on 2017/4/14.
 * 异常集中处理类
 */
@ControllerAdvice //处理controller中抛出的自定义异常
public class PermissionAdvice {

    @ExceptionHandler(value = PermissionCheckException.class)
    @ResponseStatus(value = HttpStatus.OK)
    @ResponseBody
    public String dealWithPermissionCheckException(PermissionCheckException exception){
        //System.out.println("start deal with 'PermissionCheckException' !");
        System.out.println(exception.getMessage());
        //exception.printStackTrace();
        return exception.getMessage();
    }

    @ExceptionHandler(value = NoPermissionException.class)
    @ResponseStatus(value = HttpStatus.OK)
    @ResponseBody
    public String dealWithPermissionCheckException(NoPermissionException exception){
        //System.out.println("start deal with 'NoPermissionException' !");
        System.out.println(exception.getMessage());
        //exception.printStackTrace();
        return exception.getMessage();
    }

    @ExceptionHandler(value = ParameterCanNotBeNullException.class)
    @ResponseStatus(value = HttpStatus.OK)
    @ResponseBody
    public String dealWithPermissionCheckException(ParameterCanNotBeNullException exception){
        //System.out.println("start deal with 'ParameterCanNotBeNullException' !");
        System.out.println(exception.getMessage());
        //exception.printStackTrace();
        return exception.getMessage();
    }

    @ExceptionHandler(value = ResultIsNullException.class)
    @ResponseStatus(value = HttpStatus.OK)
    @ResponseBody
    public String dealWithPermissionCheckException(ResultIsNullException exception){
        //System.out.println("start deal with 'ResultIsNullException' !");
        System.out.println(exception.getMessage());
        //exception.printStackTrace();
        return exception.getMessage();
    }

    @ExceptionHandler(value = InjectionBeanFailException.class)
    @ResponseStatus(value = HttpStatus.OK)
    @ResponseBody
    public String dealWithPermissionCheckException(InjectionBeanFailException exception){
        //System.out.println("start deal with 'InjectionBeanFailException' !");
        System.out.println(exception.getMessage());
        //exception.printStackTrace();
        return exception.getMessage();
    }
}